package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.Server;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class daily_bonus {
  private static ArrayList<ArrayList<Object>> aData17 = new ArrayList<>();
  
  private static boolean NO_MORE_GET_DATA17 = false;
  
  public static final String TOKEN = ",";
  
  public static void main(String[] a) {
    try {
      while (true)
        Server.main(null); 
    } catch (Exception exception) {
      return;
    } 
  }
  
  public static void dailybonus(L1PcInstance pc) {
    ArrayList<?> aTempData = null;
    if (!NO_MORE_GET_DATA17) {
      NO_MORE_GET_DATA17 = true;
      getData17();
    } 
    int i = 0;
    while (i < aData17.size()) {
      aTempData = aData17.get(i);
      if (pc.getday() == ((Integer)aTempData.get(0)).intValue()) {
        if ((int[])aTempData.get(1) != null && (int[])aTempData.get(2) != null) {
          int[] giveMaterials = (int[])aTempData.get(1);
          int[] giveCounts = (int[])aTempData.get(2);
          int l = 0;
          while (l < giveMaterials.length) {
            L1ItemInstance item = ItemTable.get().createItem(giveMaterials[l]);
            if (item.isStackable()) {
              item.setCount(giveCounts[l]);
            } else {
              item.setCount(1L);
            } 
            if (item != null) {
              if (pc.getInventory().checkAddItem(item, giveCounts[l]) != 0)
                return; 
              pc.getInventory().storeItem(item);
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, item.getLogName()));
            } 
            l++;
          } 
        } 
        pc.getQuest().set_step(8991, 1);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage((String)aTempData.get(3)));
        if (((Integer)aTempData.get(5)).intValue() != 0)
          pc.setpcdmg(pc.getpcdmg() + ((Integer)aTempData.get(5)).intValue()); 
        if (((Integer)aTempData.get(6)).intValue() != 0)
          pc.setReductionDmg(pc.getReductionDmg() + ((Integer)aTempData.get(6)).intValue()); 
        if (((Integer)aTempData.get(7)).intValue() != 0)
          pc.get_other().set_addhp(((Integer)aTempData.get(7)).intValue()); 
        if (((Integer)aTempData.get(8)).intValue() != 0)
          pc.get_other().set_addmp(((Integer)aTempData.get(8)).intValue()); 
        String type1 = "";
        if (pc.getQuest().get_step(8991) != 1) {
          type1 = "今日尚未签到";
        } else {
          type1 = String.valueOf(pc.getday());
        } 
        type1 = String.valueOf(pc.getday());
        pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(pc.getId(), "dailybonus", new String[] { type1 }));
      } 
      i++;
    } 
  }
  
  private static void getData17() {
    Connection con = null;
    try {
      con = DatabaseFactory.get().getConnection();
      Statement stat = con.createStatement();
      ResultSet rset = stat.executeQuery("SELECT * FROM w_每日签到控制");
      ArrayList<Object> aReturn = null;
      if (rset != null)
        while (rset.next()) {
          aReturn = new ArrayList();
          aReturn.add(0, new Integer(rset.getInt("签到几天")));
          aReturn.add(1, getArray(rset.getString("签到物品奖励"), ",", 1));
          aReturn.add(2, getArray(rset.getString("签到物品数量"), ",", 1));
          aReturn.add(3, rset.getString("完成签到显示"));
          aReturn.add(4, Integer.valueOf(0));
          aReturn.add(5, new Integer(rset.getInt("永久物攻增加")));
          aReturn.add(6, new Integer(rset.getInt("永久减伤增加")));
          aReturn.add(7, new Integer(rset.getInt("永久血量增加")));
          aReturn.add(8, new Integer(rset.getInt("永久魔量增加")));
          aData17.add(aReturn);
        }  
      SQLUtil.close(rset);
      SQLUtil.close(stat);
      SQLUtil.close(con);
    } catch (Exception exception) {}
  }
  
  private static Object getArray(String s, String sToken, int iType) {
    StringTokenizer st = new StringTokenizer(s, sToken);
    int iSize = st.countTokens();
    String sTemp = null;
    if (iType == 1) {
      int[] iReturn = new int[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        iReturn[i] = Integer.parseInt(sTemp);
        i++;
      } 
      return iReturn;
    } 
    if (iType == 2) {
      String[] sReturn = new String[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        sReturn[i] = sTemp;
        i++;
      } 
      return sReturn;
    } 
    if (iType == 3) {
      String sReturn2 = null;
      int i = 0;
      while (i < iSize) {
        sTemp = sReturn2 = st.nextToken();
        i++;
      } 
      return sReturn2;
    } 
    return null;
  }
}
